package com.sunday.fangdaifu.activity.task;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.platform.comapi.map.A;
import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.TaskDetailAdapter;
import com.sunday.fangdaifu.base.MyApplication;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.TaskDetail;
import com.sunday.fangdaifu.utils.SharePerferenceUtils;
import com.sunday.fangdaifu.widgets.ReplyWindow;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.R.attr.key;

/**
 * Created by yun_sheng on 2017/7/4.
 */

public class TaskDetailActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_level)
    TextView tvLevel;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.img)
    ImageView img;
    @Bind(R.id.ly_info)
    RelativeLayout lyInfo;
    @Bind(R.id.img1)
    ImageView img1;
    @Bind(R.id.ly_addr)
    RelativeLayout lyAddr;
    @Bind(R.id.ly)
    RelativeLayout ly;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.checkInfo)
    TextView checkInfo;
    @Bind(R.id.btn)
    TextView btn;
    @Bind(R.id.ly_history)
    RelativeLayout lyHistory;
    @Bind(R.id.bottom)
    LinearLayout bottom;
    @Bind(R.id.btn0)
    TextView btn0;
    @Bind(R.id.addr)
    TextView addr;

    private TaskDetailAdapter adapter;
    private LinearLayoutManager manager;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id", 0);
        initDate();
    }

    private void initDate() {
        showLoadingDialog();
        ApiClient.getApiService().taskDetail(id, this, new TypeToken<ResultDO<TaskDetail>>() {
        }.getType());
    }

    @OnClick({R.id.ly_info, R.id.ly_addr, R.id.ly_history, R.id.btn0, R.id.btn})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ly_info:
                Intent intent2 = new Intent(mContext, HouseDetailActivity.class);
                intent2.putExtra("taskDetail", taskDetail);
                startActivity(intent2);
                break;
            case R.id.ly_addr:
                Intent intent3 = new Intent(mContext, MapActivity.class);
                intent3.putExtra("longitude", taskDetail.getLongitude());
                intent3.putExtra("latitude", taskDetail.getLatitude());
                intent3.putExtra("addr", taskDetail.getAddress());
                startActivity(intent3);
                break;
            case R.id.ly_history:
                Intent intent = new Intent(mContext, HistoryTaskListActivity.class);
                intent.putExtra("id", houseId);
                startActivity(intent);
                break;
            case R.id.btn0:
                ReplyWindow window = new ReplyWindow(mContext);
                window.showPopupWindow(btn0);
                window.setOnSelectDirection(new ReplyWindow.OnSelectDirection() {
                    @Override
                    public void selectDirection(String content) {
                        showLoadingDialog();
                        ApiClient.getApiService().replyDirect(taskDetail.getDetailId(), content, TaskDetailActivity.this, new TypeToken<ResultDO>() {
                        }.getType());
                    }
                });
                break;
            case R.id.btn:
                if (taskDetail.getWriteStatus() == 1) {
                    //填写报告
                    Intent intent1 = new Intent(mContext, CommitTaskActivity.class);
                    intent1.putExtra("taskDetail", taskDetail);
                    intent1.putExtra("isNeedReadDraft", true);
                    startActivity(intent1);
                } else if (taskDetail.getWriteStatus() == 2) {
                    return;
                } else {
                    //开始任务
                    if (ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            ((CommitTaskActivity) mContext).requestPermissions(new String[]{ACCESS_COARSE_LOCATION},
                                    0);
                        }
                        return;
                    }
                    MyApplication.getInstance().requestLocation();
                    if (MyApplication.getInstance().isLocatioSuc()) {
                        String latitude = SharePerferenceUtils.getIns().getString("latitude", "");
                        String longitude = SharePerferenceUtils.getIns().getString("longitude", "");
                        ApiClient.getApiService().changeTaskStatus(taskDetail.getDetailId(), 1, latitude, longitude, this, new TypeToken<ResultDO>() {
                        }.getType());
                    }
                }

                break;
        }
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        dissMissDialog();
        switch (key) {
            case Api.TASK_DETAIL:
                ResultDO<TaskDetail> resultDO = (ResultDO<TaskDetail>) result;
                if (resultDO.getCode() == 0) {
                    taskDetail = resultDO.getResult();
                    showDate(taskDetail);
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage() + "");
                }
                break;
            case Api.CHANGE_TASK_STATUS:
                ResultDO resultDO1 = (ResultDO) result;
                if (resultDO1.getCode() == 0) {
                    initDate();
                } else {
                    ToastUtils.showToast(mContext, resultDO1.getMessage() + "");
                }
                break;
            case Api.REPLY_DIRECT:
                ResultDO resultDO2 = (ResultDO) result;
                if (resultDO2.getCode() == 0) {
                    ToastUtils.showToast(mContext, "回复成功");
                } else {
                    ToastUtils.showToast(mContext, resultDO2.getMessage() + "");
                }
                break;

        }

    }

    private int houseId;
    private TaskDetail taskDetail;

    private void showDate(TaskDetail taskDetail) {
        tvTitle.setText(taskDetail.getHouseName());
        tvLevel.setText(taskDetail.getLevel());
        tvType.setText(taskDetail.getTypeName());
        checkInfo.setText(taskDetail.getCheckInfo());
        addr.setText(taskDetail.getAddress());
        houseId = taskDetail.getHouseId();
        manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        adapter = new TaskDetailAdapter(mContext, taskDetail.getImageList());
        recyclerView.setAdapter(adapter);
        switch (taskDetail.getLevel()) {
            case "A级":
                tvLevel.setBackgroundResource(R.drawable.border_66e04a);
                tvLevel.setTextColor(mContext.getResources().getColor(R.color._66e04a));
                break;
            case "B级":
                tvLevel.setBackgroundResource(R.drawable.border_ffa04c);
                tvLevel.setTextColor(mContext.getResources().getColor(R.color._ffa04c));
                break;
            case "C级":
                tvLevel.setBackgroundResource(R.drawable.border_ff8519);
                tvLevel.setTextColor(mContext.getResources().getColor(R.color._ff8519));
                break;
            case "D级":
                tvLevel.setBackgroundResource(R.drawable.border_ff230a);
                tvLevel.setTextColor(mContext.getResources().getColor(R.color._ff230a));
                break;
            default:
                tvLevel.setBackgroundResource(R.drawable.border_66e04a);
                tvLevel.setTextColor(mContext.getResources().getColor(R.color._66e04a));
        }
        switch (taskDetail.getTypeName()) {
            case "居民反馈":
                bottom.setVisibility(taskDetail.getWriteStatus() == 2 ? View.GONE : View.VISIBLE);
                btn0.setVisibility(View.VISIBLE);
                tvType.setBackgroundResource(R.drawable.border_ff8519);
                tvType.setTextColor(mContext.getResources().getColor(R.color._ff8519));
                break;
            case "常规巡检":
                btn0.setVisibility(View.GONE);
                btn.setText(taskDetail.getWriteStatus() == 2 ? "已完成" : taskDetail.getWriteStatus() == 1 ? "填写报告" : "开始任务");
                tvType.setBackgroundResource(R.drawable.border_7cb2ff);
                tvType.setTextColor(mContext.getResources().getColor(R.color._70bcff));
                break;
            case "分配巡检":
                btn0.setVisibility(View.GONE);
                tvType.setBackgroundResource(R.drawable.border_66e04a);
                btn.setText(taskDetail.getWriteStatus() == 2 ? "已完成" : taskDetail.getWriteStatus() == 1 ? "填写报告" : "开始任务");
                tvType.setTextColor(mContext.getResources().getColor(R.color._66e04a));
                break;
            default:
                btn0.setVisibility(View.GONE);
                tvType.setBackgroundResource(R.drawable.border_66e04a);
                tvType.setTextColor(mContext.getResources().getColor(R.color._66e04a));
        }

    }

    @Override
    public void onFailure() {
        dissMissDialog();
        ToastUtils.showToast(mContext, "服务器异常！");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initDate();
    }
}
